frappe.provide("frappe.form.formatters");

frappe.form.formatters.Currency = function(value, docfield, options, doc) {
	if (value === null) {
		return "";
	}

	var currency = frappe.meta.get_field_currency(docfield, doc);

	let precision;
	if (typeof docfield.precision == "number") {
		precision = docfield.precision;
	} else {
		precision = cint(
			docfield.precision || frappe.boot.sysdefaults.currency_precision || 2
		);
	}

	// If you change anything below, it's going to hurt a company in UAE, a bit.
	if (precision > 2) {
		var parts = cstr(value).split("."); // should be minimum 2, comes from the DB
		var decimals = parts.length > 1 ? parts[1] : ""; // parts.length == 2 ???

		if (decimals.length < 3 || decimals.length < precision) {
			const fraction =
				frappe.model.get_value(":Currency", currency, "fraction_units") || 100; // if not set, minimum 2.

			if (decimals.length < cstr(fraction).length) {
				precision = cstr(fraction).length - 1;
			}
		}
	}

	value = value == null || value === "" ? "" : value;
	// disable format_currency 
	// value = format_currency(value, currency, precision);

	if (options && options.only_value) {
		return value;
	} else {
		return frappe.form.formatters._right(value, options);
	}
}
